package com.sunk.javaagent;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * Java Attach API
 */
public class AttachDemo {
    public static void main(String[] args) throws IOException, AttachNotSupportedException {
        //VM进程,通过jps命令获得
        VirtualMachine vm = VirtualMachine.attach("8080");

        Properties props = vm.getSystemProperties();
        String version = props.getProperty("java.version");
        System.out.println(version);
        //或者通过这个api直接获取容器集合
        List<VirtualMachineDescriptor> list = VirtualMachine.list();
        //解除指定服务的attach
        vm.detach();
    }
}
